<?php

/**
 * 导出参与好物亲测的用户
 */
ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\GoodsTestActivity\FineGoodsJoinModel;
use Biz\model\GoodsTestActivity\FineGoodsJoinRecordModel;
use Biz\model\GoodsTestActivity\FineGoodsModel;
use Biz\service\GoodsTestActivity\BigDataService;
use Doraemon\service\User;

//体验中/名单已产出
$condition2 = FineGoodsModel::getConditionByState(FineGoodsModel::STATE_EXPERIENCING);
$list1 = FineGoodsModel::selectList($condition2);
$fineGoodsIds1 = array_column($list1, 'id');
//报告已产出
$condition2 = FineGoodsModel::getConditionByState(FineGoodsModel::STATE_OUTPUT_REPORT);
$list2 = FineGoodsModel::selectList($condition2);
$fineGoodsIds2 = array_column($list2, 'id');

$fineGoodsIds = array_merge($fineGoodsIds1, $fineGoodsIds2);
var_dump($fineGoodsIds);
$list = array_merge($list1, $list2);
$list = array_column($list, null, 'id');

$recoredList = FineGoodsJoinRecordModel::all(['fine_goods_id' => $fineGoodsIds]);
$uids = array_column($recoredList, 'uid');
$recoredList = array_column($recoredList, null, 'id');
list($scoreMap, $tagList) = BigDataService::getScoreMap($uids);
$userList = $uids ? User::getBatchBasicUserInfo($uids) : [];
$userList = $userList ? array_column($userList, null, 'uid') : [];
$joinList = FineGoodsJoinModel::all(['fine_goods_id' => $fineGoodsIds, 'join_status' => FineGoodsJoinModel::JOIN_STATUS_PREFER]);
//申请用户
$recoredData = [];
foreach ($recoredList as $row) {
    $tagsArr = isset($scoreMap[$row['uid']]) ? $scoreMap[$row['uid']] : [];
    $recoredData[] = [
        'fine_goods_id' => $row['fine_goods_id'],
        'activity_title' => $list[$row['fine_goods_id']]['activity_title'],
        'uid' => $row['uid'],
        'nick' => isset($userList[$row['uid']]) ? $userList[$row['uid']]['nick'] : '',
        'tags' => isset($tagList[$row['uid']]) ? $tagList[$row['uid']]['tag'] : '',
        'fans' => $row['fans_num']
    ];
}
report('申请用户', $recoredData);
//已选用户
$joinData = [];
foreach ($joinList as $row) {
    $tagsArr = isset($scoreMap[$row['uid']]) ? $scoreMap[$row['uid']] : [];
    $recoredInfo = $recoredList[$row['record_id']];
    $joinData[] = [
        'fine_goods_id' => $row['fine_goods_id'],
        'activity_title' => $list[$row['fine_goods_id']]['activity_title'],
        'uid' => $row['uid'],
        'nick' => isset($userList[$row['uid']]) ? $userList[$row['uid']]['nick'] : '',
        'tags' => isset($tagList[$row['uid']]) ? $tagList[$row['uid']]['tag'] : '',
        'fans' => $recoredInfo['fans_num']
    ];
}
report('已选用户', $joinData);

function report($fileTitle, $data)
{
    var_dump(count($data));
    $title = ['活动id', '活动名称', '用户uid', '用户昵称', '用户标签', '粉丝数'];
    $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
    $sheet = $spreadsheet->getActiveSheet();
    $titCol = 'A';
    foreach ($title as $key => $value) {
        // 单元格内容写入
        $sheet->setCellValue($titCol . '1', $value);
        $titCol++;
    }
    $sheet->getColumnDimension('A')->setWidth(15);
    $sheet->getColumnDimension('B')->setWidth(25);
    $sheet->getColumnDimension('C')->setWidth(15);
    $sheet->getColumnDimension('D')->setWidth(30);
    $sheet->getColumnDimension('E')->setWidth(50);
    $sheet->getColumnDimension('F')->setWidth(30);
    $row = 2; // 从第二行开始
    foreach ($data as $item) {
        $dataCol = 'A';
        foreach ($item as $value) {
            // 单元格内容写入
            $sheet->setCellValue($dataCol . $row, $value);
            $dataCol++;
        }
        $row++;
    }
    $fileName = $fileTitle . '.xlsx';
    $filePath = dirname(__FILE__) . DIRECTORY_SEPARATOR . $fileName;
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="' . $fileName . '"');
    header('Cache-Control: max-age=0');
    header('Cache-Control: max-age=1');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
    header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header('Pragma: public'); // HTTP/1.0
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save($filePath);
    var_dump($fileTitle . '导出成功');
}